// pages/order/orderComment/orderComment.js
const app = getApp();
Page({
  data: {
    staticImg: app.globalData.staticImg,
    current: 0,
    taste: true,
    time: true,
    environment: true,
    userStars: [
      "../../../images/xxk.png",
      "../../../images/xxk.png",
      "../../../images/xxk.png",
      "../../../images/xxk.png",
      "../../../images/xxk.png",
    ],
    wjxScore: 0,
    // textarea
    min: 5, //最少字数
    max: 300, //最多字数 (根据自己需求改变) 
    pics: [],
    // 用户个人信息
    userInfo: {
      avatar: '',
      user_name: '',
      user_id:''
    },
    commentList: [],
    comment_content: "",
    isCanShow: 0,
  },

  // 星星点击事件
  starTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index; // 获取当前点击的是第几颗星星
    var tempUserStars = this.data.userStars; // 暂存星星数组
    var len = tempUserStars.length; // 获取星星数组的长度
    for (var i = 0; i < len; i++) {
      if (i <= index) { // 小于等于index的是满心
        tempUserStars[i] = "../../../images/xx.png";
        that.setData({
          wjxScore: i + 1,
        })

      } else { // 其他是空心
        tempUserStars[i] = "../../../images/xxk.png"
      }
    }
    // 重新赋值就可以显示了
    that.setData({
      userStars: tempUserStars
    })
  },

  // 标签
  label: function (e) {
    console.log(e)
    var that = this;
    that.setData({
      taste: !e.currentTarget.dataset.index
    })
  },
  label1: function (e) {
    console.log(e)
    var that = this;
    that.setData({
      time: !e.currentTarget.dataset.index
    })
  },
  label2: function (e) {
    console.log(e)
    var that = this;
    that.setData({
      environment: !e.currentTarget.dataset.index
    })
  },

  // 留言
  inputs: function (e) {
    // 获取输入框的内容
    this.data.inputValue = e.detail.value;
    // 获取输入框内容的长度
    var len = parseInt(this.data.inputValue.length);
    //最多字数限制
    if (len > this.data.max) return;
    // 当输入框内容的长度大于最大长度限制（max)时，终止setData()的执行
    this.setData({
      currentWordNumber: len //当前字数  
    });
  },

  // 图片
  choose: function (e) { //这里是选取图片的方法
    var that = this;
    var pics = that.data.pics;
    console.log("pics", pics.length)
    wx.chooseImage({
      count: 3-pics.length, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        var imgsrc = res.tempFilePaths;
        pics = pics.concat(imgsrc);
        console.log(pics);
        // console.log(imgsrc);
        that.setData({
          pics: pics,
          // console.log(pics),
        });
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })

  },

  // uploadimg: function () { //这里触发图片上传的方法
  //   var pics = this.data.pics;
  //   console.log(pics);
  //   app.uploadimg({
  //     url: 'https://xiaxueruo.com/api/comment/add?image='+ JSON.stringify(this.data.pics), //这里是你图片上传的接口
  //     path: pics //这里是选取的图片的地址数组
  //   });
  // },

  onLoad: function (options) {
    this.checkIsCanShow();
    // 获取用户登录信息
    let user_id = wx.getStorageSync('user_id');
    let user_name = wx.getStorageSync('user_name');
    let avatar = wx.getStorageSync('avatar');
    this.setData({
      userInfo: {
        avatar: avatar,
        user_name: user_name,
        user_id:user_id
      }
    });

    this.setData({
      detail_id: options.detail_id,
      ware_id: options.ware_id
    });
    this.commentList(options.detail_id,options.ware_id)
    console.log('------detail_id',options.detail_id)
    console.log('------ware_id',options.ware_id)
    console.log(app.globalData.wareList)
    let wareList = app.globalData.wareList;  
    console.log('-----------------文章内容',wareList)   
    let wareinfo = '';
    for (let i = 0; i < wareList.length;i++){
      console.log('---------i',wareList[i].ware_id)
      if(+options.ware_id === wareList[i].ware_id){
        wareinfo = wareList[i]
      }
    }  
    this.setData({
      wareinfo: wareinfo,
    });

  },

  // 删除图片
  deleteImg: function (e) {
    var pics = this.data.pics;
    var index = e.currentTarget.dataset.index;
    pics.splice(index, 1);
    this.setData({
      pics: pics
    });
  },

  // 预览图片
  previewImg: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var pics = this.data.pics;
    wx.previewImage({
      //当前显示图片
      current: pics[index],
      //所有图片
      urls: pics
    })
  },

   	/**
	 * 评论
	 */
  commentList(detail_id,ware_id){
    let that = this
    console.log('------------9',detail_id);
    console.log('------------9',ware_id);
    console.log('this.data',this.data)
    wx.request({
      url: 'https://order.xiaxueruo.com/api/comment/list?detail_id='+detail_id+"&ware_id="+ware_id,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',//get或者post
      
      success: function (res) {
        console.log(".....",res)//接口返回值
        that.setData({
          commentList: res.data.data.commentList
        })
      }
    })
  },

  gitComment: function () {
    var input = this.data.inputValue
    console.log("input", input)
    console.log("this.data.commentList", this.data.commentList)
    // console.log("this.data.commentList.detail_id", this.data.commentList.detail_id)
    if (input == "" || input == undefined) {
      wx.showToast({
        title: '没有输入内容哦~',
        icon: 'error',
        duration: 2000
      })
    } else {
      // this.commentList = this.data.commentList,
      console.log("名称：", wx.getStorageSync('user_name'))
      console.log("内容：", this.data.inputValue)
      console.log("商品id：", this.data.wareinfo.ware_id)
      console.log("头像：", wx.getStorageSync('avatar'))
      console.log("taste：", this.data.taste)
      console.log("time：", this.data.time)
      console.log("environment：", this.data.environment)
      console.log("wjxScore：", this.data.userStars.length)
      console.log("img：", JSON.stringify(this.data.pics))
      console.log("data：", this.data)

      let _this = this;
      wx.request({
        url: 'https://order.xiaxueruo.com/api/comment/add?ware_id=' + this.data.wareinfo.ware_id + "&comment_content=" + this.data.inputValue + "&user_name=" + wx.getStorageSync('user_name') + "&avatar=" + wx.getStorageSync('avatar') + '&user_id=' + wx.getStorageSync('user_id') + "&taste=" + this.data.taste + "&time=" + this.data.time + "&environment=" + this.data.environment + "&score=" + this.data.userStars.length + "&detail_id=" + this.data.detail_id,
        data: {
          image: JSON.stringify(this.data.pics)
        },
        success: function (res) {
          console.log('res----',res)
          // _this.data.userInfo.avatarUrl=res.data.data.comment.avatar
          // console.log(_this.data.articleinfo.article_id)
          // wx.setStorageSync('comment_id', res.data.data.commentList.comment_id)
          _this.data.commentList.unshift(res.data.data.commentList)
          _this.setData({
            commentList: _this.data.commentList,
            inputValue: "",
          })
         
          wx.navigateTo({
            url: '../orderComment/gitComment/gitComment',
          })
          wx.showToast({
            title: '评价成功',
            icon: 'success',
            duration: 1500,
          })
        }
      })
    }
  },

  /**
   * 获取接口数据，控制是否显示敏感信息
   */
  checkIsCanShow() {
    wx.request({
      url: 'https://order.xiaxueruo.com/api/order/isCanShow/',
      success: res => {
        this.setData({
          isCanShow: res.data
        })
      }
    })
  },


})